import {
  applicationdetail,
  isFrozen,
  findPscode
} from "../../server/api";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    ordersid: "",
    show: false,
    hxvalue: "",
    beforeClose: null,
    avt:
      "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
    list: [
      // {
      //   img: "/icon/商家/商家端-财务上报x280.png",
      //   title: "财务上报",
      // },
      {
        img: "/icon/商家/商家端-财务上报x280.png",
        title: "意见反馈",
      },
      {
        img: "/icon/商家/商家端-输入核销x280.png",
        title: "输入核销",
      },
      {
        img: "/icon/商家/商家端-扫码核销x280.png",
        title: "扫码核销",
      },
      {
        img: "/icon/商家/商家端-核销记录x280.png",
        title: "核销记录",
      },
    ],
    data: [
      // {
      //   icon: "/icon/我的-归属地管理x64.png",
      //   name: "归属地管理",
      // },
      {
        icon: "/icon/我的-积分记录x64.png",
        name: "积分核销记录",
      },
      {
        icon: "/icon/我的-关于我们x64.png",
        name: "关于我们",
      },
    ],
    phone: "",
    map: {},
    status: "3",
    xj: "",
    username: "",
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  async onShow() {
    this.getTabBar().toggleMenu(2);
    this.getTabBar().init();
    const res = await applicationdetail();
    if (res.code == 200) {
      const data = res.result;
      this.setData({
        xj: data.businessHonor,
        phone: this.maskPhoneNumber(data.businessPhone),
        username:data.businessName
      });
    }
  },
    //输入核销确认
    async getUserInfo() {
      this.setData({
        beforeClose: (action) =>
          new Promise(async (resolve) => {
            console.log(action, "aciton");
            if (action === "confirm") {
              if (this.data.hxvalue != "") {
                const val = await isFrozen();
                if (val.result == "false") {
                  const params = {
                    psCode: this.data.hxvalue,
                  };
                  const data = await findPscode(params);
                  if (data.code == 200) {
                    const obj = JSON.stringify(data.result);
                    wx.navigateTo({
                      url: `/subPackagesB/pages/vercode/vercode?data=${obj}&hxvalue=${this.data.hxvalue}`,
                    });
                    this.setData({ show: false, hxvalue: "" });
                  }
                } else {
                  wx.showToast({
                    title: "请输入核销码",
                    icon: "none",
                  });
                }
              } else {
                wx.showToast({
                  title: "您的账号已冻结,请联系管理员!",
                  icon: "none",
                });
              }
  
              resolve(false);
            } else if (action === "cancel") {
              this.setData({ hxvalue: "" });
              // 取消按钮
              resolve(true);
            }
          }),
      });
    },
    //扫码核销
    scan() {
      wx.scanCode({
        success: async (res) => {
          //判断商家账号是否已被冻结
          const val = await isFrozen();
          if (val.result == "false") {
            this.setData({ ordersid: res.result });
            const params = {
              psCode: res.result,
            };
            const data = await findPscode(params);
            if (data.code == 200) {
              const obj = JSON.stringify(data.result);
              wx.navigateTo({
                url: `/subPackagesB/pages/scancode/scancode?data=${obj}&ordersid=${res.result}`,
              });
            }
          } else {
            wx.showToast({
              title: "您的账号已冻结,请联系管理员!",
              icon: "none",
            });
          }
        },
        fail: (err) => {
          console.error(err);
        },
      });
    },
      //输入核销
  verification() {
    this.setData({ show: true });
  },
  //常用功能跳转页面
  async detail(e) {
    const { index } = e.currentTarget.dataset;
    switch (index) {
      case 0:
        // wx.navigateTo({
        //   url: "/subPackagesB/pages/financial/financial",
        // });
        wx.navigateTo({
          url: "/subPackagesB/pages/feedback/feedback",
        });
        break;
      case 1:
        this.verification()
        break;
      case 2:
        this.scan()
        break;
      case 3:
        wx.setStorageSync('count', 6)
        wx.reLaunch({
          url: `/pages/verification/verification?type=6`,
        })
        break;
      default:
        break;
    }
  },
  //关于我们跳转页面
  pages(e) {
    const { index } = e.currentTarget.dataset;
    switch (index) {
      // case 0:
      //   wx.navigateTo({
      //     url: "/subPackagesA/pages/belonging/belonging",
      //   });
      //   break;
      case 0:
        wx.setStorageSync('count', 6)
        wx.reLaunch({
          url: `/pages/verification/verification?type=6`,
        })
        // wx.switchTab({
        //   url: "/pages/verification/verification",
        // });
        break;
      default:
        wx.navigateTo({
          url: "/subPackagesA/pages/aboutus/aboutus",
        });
        break;
    }
  },
  //将手机号中间四位替换掉
  maskPhoneNumber(phoneNumber) {
    return phoneNumber.replace(/(\d{3})\d{4}(\d{4})/, "$1****$2");
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
});
